﻿/*----------------------------------------------------------------
// Copyright (C) 2011 上海
// 版权所有。 
//
// 文件名：DataAccessGroupInformation
// 文件功能描述：班组数据访问层
//
// 
// 创建标识：2012-08-06 王冲
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DbAdapter.sqldbAdapter;
using System.Data.SqlClient;

namespace DAO.WaiGaoQiao
{
    public class DataAccessGroupInformation
    {
        /// <summary>
        /// 获取部门列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public DataSet GetdeptName()
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select Dep_ID,Dep_No,deptName from File_Department order by Dep_ID";
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 获取班组信息
        /// </summary>
        /// <returns></returns>
        public DataSet SelGroupInfo(int pagesize, int startindex, string strquery, out int count)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select * from (select ROW_NUMBER() OVER(ORDER BY groupId ASC) rownumber,* " +
                               "from GroupInformationY3 a inner join File_Department b on a.departmentId=b.Dep_ID where 1=1 " + strquery + ") B "
                             + "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString();
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                object obj = db.ExecuteScalar(conn, CommandType.Text, "select count(*) from GroupInformationY3 a where 1=1 " + strquery);
                count = Convert.ToInt32(obj);
                return ds;
            }
        }

        /// <summary>
        /// 保存班组(新增界面下)
        /// </summary>
        /// <param name="supp"></param>
        /// <returns></returns>
        public int SaveGroupInformation(object[] supp)
        {
            sqldb db = new sqldb();
            string query = "select MAX(groupid)+1 as groupid,Max(cast(groupno as int))+1 as groupno from GroupView";
            DataSet ds = db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);

            query = "insert into GroupInformationY3(groupId,groupNo,groupName,departmentId,ISDELETED) Values('" +
                ds.Tables[0].Rows[0]["groupid"] + "','" + ds.Tables[0].Rows[0]["groupno"] + "','" + supp[0] + "','" + supp[1] + "',0)";
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }

        /// <summary>
        /// 根据编号获取一条班组信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataSet GetGroupInformationInfoOne(string id)
        {
            sqldb db = new sqldb();
            string query = "select * from GroupInformationY3 where groupId = '" + id + "'";
            return db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
        }

        /// <summary>
        /// 保存班组(修改界面下)
        /// </summary>
        /// <param name="supp"></param>
        /// <returns></returns>
        public int SaveGroupInformationEdit(object[] supp)
        {
            sqldb db = new sqldb();
            string query = "update GroupInformationY3 set groupName='" + supp[1] + "',departmentId='" + supp[2] + "' where groupId=" + supp[0];
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }

        /// <summary>
        /// 删除班组
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int DeleteGroupInformation(string id)
        {
            sqldb db = new sqldb();
            string query = "select count(*) from File_Personnel where GroupIdDept in(" + id + ")";
            int rowcount = Convert.ToInt32(db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query));
            if (rowcount > 0)
            {
                rowcount = 0;
            }
            else
            {
                query = "Delete from GroupInformationY3 where groupId in(" + id + ")";
                rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            }
            return rowcount;
        }
    }
}
